ggplot2 facets allow to split a plot into panels
depending on one (or more) categorical variables.
ggplot2 themes configure plot graphical settings: colors,
fonts, …
➡️Go to RStudio Cheatsheets/Data Visualization Cheatsheet/Panel Scales to check commands for facets and themes.
ggplot is a function and it returns an object
(of class ggplot) representing a plot.
This object can be stored in a variable.
Notice, that no plot is shown by the following code:
p <- ggplot( pulse ) +
aes( x = weight, y = height, color = exercise, shape = gender ) +
geom_point( size = 3, alpha = 0.8 )
Only once the variable is printed the plot is shown.
p
Such a plot object may also be saved to a file with
the ggsave function.
In Help you may find how to specify width, height and dpi
resolution of the image.
Multiple file formats are supported and by default they are detrmined
from the filename.
For example, to save plot p in PNG format:
ggsave( "my_plot.png", plot = p )
It is possible to control the dimensions of the plots in your R
Markdown report.
Try to add additional options to the first line of a chunk which
produces a plot.
Follow this example (the dimensions fig.width and
fig.height are specified in inches and dpi
sets resolution in pixels per inch):
```{r fig.width=3,fig.height=2,dpi=75}
p
```
Let’s use the plot stored in the variable p and combine
it with a facet command.
Compare the plot below to the plot above.
Points corresponding to different levels of exercise are
shown in separate plot panels.
Note, that a formula of a form y_var ~ x_var is used to
specify which variables should define panels in the vertical
(y_var) and horizontal (x_var)
directions.
. is used when there should be no panels in a given
direction.
p + facet_grid( . ~ exercise )
ggplot2 allows detailed configuration of plots, far
beyond the scope of this course.
We advise to use google search with phrases like “ggplot2
rotate axis labels”.
Let’s again use the plot stored in the variable p and
combine it with general themes.
Try each of the following lines and observe the effects:
p + theme_minimal()
p + theme_dark()
p + theme_bw()
Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC